<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'btn:add:cailiaoxuqiu'">
          <Icon icon="fluent:add-12-filled" /> {{ t('创建材料需求计划') }}
        </a-button>
        <a-button type="default" @click="handleNotice()" v-auth="'btn:need:pmMaterialneed:notice'">
          <Icon icon="ant-design:notification-outlined" /> {{ t('送货通知') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.name }}
        </a>
      </template>

      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.projectno }} -->
        <a @click="handleDetailsForm(record)" >
          {{ record.projectno }}
        </a>
        
      </template>

      
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <shform @register="registershDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMaterialNeedPmMaterialneedList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    pmMaterialneedDelete,
    pmMaterialneedListData,
  } from '/@/api/material/need/pmMaterialneed';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import shform from './shform.vue';

  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('material.need.pmMaterialneed');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('材料需求计划管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工程编码'),
        field: 'projectno',
        component: 'Input',
      },
      {
        label: t('工程名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('创建时间'),
        field: 'createtime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
        ifShow: false,
      },
      {
        label: t('设计变更编号'),
        field: 'sno',
        component: 'Input',
      },
      {
        label: t('分单编号'),
        field: 'fdno',
        component: 'Input',
      },
      {
        label: t('提交时间'),
        field: 'submittime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('需求时间'),
        field: 'needdate',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('ffile'),
        field: 'ffile',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('审核人'),
        field: 'checker',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('审核时间'),
        field: 'checktime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
        ifShow: false,
      },
      {
        label: t('计划/收货'),
        field: 'needorreceive',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('remark'),
        field: 'remark',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('通知送货时间'),
        field: 'notedate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
        ifShow: false,
      },
      {
        label: t('送货备注'),
        field: 'noteremark',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('noteoper'),
        field: 'noteoper',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('通知送货操作时间'),
        field: 'noteoptime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
        ifShow: false,
      },
      {
        label: t('isreceive'),
        field: 'isreceive',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('notepic'),
        field: 'notepic',
        component: 'Input',
        ifShow: false,
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('工程编码'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 100,
      align: 'left',
      slot: 'projectno',
    },
    {
      title: t('工程名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 200,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createtime',
      key: 'a.createtime',
      sorter: true,
      width: 130,
      align: 'center',
      ifShow: false,
    },
    {
      title: t('设计变更编号'),
      dataIndex: 'sno',
      key: 'a.sno',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('分单编号'),
      dataIndex: 'fdno',
      key: 'a.fdno',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('提交时间'),
      dataIndex: 'submittime',
      key: 'a.submittime',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('需求时间'),
      dataIndex: 'needdate',
      key: 'a.needdate',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('ffile'),
      dataIndex: 'ffile',
      key: 'a.ffile',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('审核人'),
      dataIndex: 'checker',
      key: 'a.checker',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('审核时间'),
      dataIndex: 'checktime',
      key: 'a.checktime',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('计划/收货'),
      dataIndex: 'needorreceive',
      key: 'a.needorreceive',
      sorter: true,
      width: 130,
      align: 'center',
      ifShow: false,
    },
    {
      title: t('remark'),
      dataIndex: 'remark',
      key: 'a.remark',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('通知送货时间'),
      dataIndex: 'notedate',
      key: 'a.notedate',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('送货备注'),
      dataIndex: 'noteremark',
      key: 'a.noteremark',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('noteoper'),
      dataIndex: 'noteoper',
      key: 'a.noteoper',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('通知送货操作时间'),
      dataIndex: 'noteoptime',
      key: 'a.noteoptime',
      sorter: true,
      width: 130,
      align: 'center',
      ifShow: false,
    },
    {
      title: t('isreceive'),
      dataIndex: 'isreceive',
      key: 'a.isreceive',
      sorter: true,
      width: 130,
      align: 'center',
      ifShow: false,
    },
    {
      title: t('notepic'),
      dataIndex: 'notepic',
      key: 'a.notepic',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('审批状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'bpm_biz_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 100,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑材料需求计划'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'btn:edit:cailiaoxuqiu',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除材料需求计划'),
        popConfirm: {
          title: t('是否确认删除材料需求计划'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'btn:delete:cailiaoxuqiu',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
    ],
  };

  const [registershDrawer, { openDrawer: openshDrawer }] = useDrawer();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: pmMaterialneedListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'radio',
    },
    immediate: false,
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleNotice() {
    if (getSelectRows().length) {
      // let selIds = ref('');
      // getSelectRows().forEach((item) => {
      //   selIds.value += item.id + ',';
      // });
      // console.log(selIds.value);

      openshDrawer(true, { id: getSelectRows()[0].id });
      // openbhgDrawer(true,{selIds:selIds.value})
      // 接口
    } else {
      showMessage('请先选择一条数据！');
    }
  }

  async function handleDelete(record: Recordable) {
    const res = await pmMaterialneedDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'need_plan', bizKey: record.id });
  }

  onMounted(async () => {
    handleProjecte();
  });

  async function handleProjecte() {
    await getForm().setFieldsValue({
      projectno: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      projectno: userStore.getProjecte.code,
    });
  }
</script>
